import tkinter as tk
from tkinter import filedialog, messagebox
import socket
import json
import os

class ScreenSplitAssistTester:
    def __init__(self, root):
        self.root = root
        self.root.title("ScreenSplitAssist_tester")

        # 屏幕名称输入框
        self.screen_name_label = tk.Label(root, text="屏幕名称:")
        self.screen_name_label.pack()
        self.screen_name_entry = tk.Entry(root)
        self.screen_name_entry.insert(0, "DisplayPort-0")
        self.screen_name_entry.pack()

        # 屏幕数量下拉框
        self.screen_count_label = tk.Label(root, text="屏幕数量:")
        self.screen_count_label.pack()
        self.screen_count_var = tk.StringVar(root)
        self.screen_count_var.set("9")
        self.screen_count_options = ["1", "4", "6", "9"]
        self.screen_count_menu = tk.OptionMenu(root, self.screen_count_var, *self.screen_count_options)
        self.screen_count_menu.pack()

        # 打开按钮
        self.open_button = tk.Button(root, text="打开", command=self.open_main_program)
        self.open_button.pack()

        # 图片选择按钮
        self.select_images_button = tk.Button(root, text="选择图片", command=self.select_images)
        self.select_images_button.pack()

        # 图片预览区域
        self.image_preview_frame = tk.Frame(root)
        self.image_preview_frame.pack()

        # 图片清空按钮
        self.clear_images_button = tk.Button(root, text="清空图片", command=self.clear_images)
        self.clear_images_button.pack()

        # 发送按钮
        self.send_button = tk.Button(root, text="发送", command=self.send_images, state=tk.DISABLED)
        self.send_button.pack()

        self.selected_images = []

    def open_main_program(self):
        screen_name = self.screen_name_entry.get()
        screen_count = self.screen_count_var.get()
        main_program_path = os.path.join(os.path.dirname(os.getcwd()), "ScreenSplitAssist")
        command = f"{main_program_path} targetDisplayScreenName={screen_name} targetDisplayScreenCount={screen_count}"
        os.system(command)

    def select_images(self):
        file_paths = filedialog.askopenfilenames(filetypes=[("JPEG Files", "*.jpg"), ("PNG Files", "*.png")])
        if file_paths:
            self.selected_images = list(file_paths)
            self.update_image_preview()
            self.update_send_button_state()

    def update_image_preview(self):
        # 清空之前的预览图
        for widget in self.image_preview_frame.winfo_children():
            widget.destroy()

        for index, image_path in enumerate(self.selected_images, start=1):
            # 这里简单用标签显示图片路径代替实际图片预览
            label = tk.Label(self.image_preview_frame, text=f"{index}. {image_path}")
            label.pack()

    def clear_images(self):
        self.selected_images = []
        self.update_image_preview()
        self.update_send_button_state()

    def update_send_button_state(self):
        screen_count = int(self.screen_count_var.get())
        if len(self.selected_images) == screen_count:
            self.send_button.config(state=tk.NORMAL)
        else:
            self.send_button.config(state=tk.DISABLED)

    def send_images(self):
        screen_count = int(self.screen_count_var.get())
        if len(self.selected_images) == screen_count:
            data = [{"zoneIndex": i + 1, "imageUri": image_path} for i, image_path in enumerate(self.selected_images)]
            try:
                client_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
                socket_path = "/tmp/com.zltongxue.screenSplitAssist"
                client_socket.connect(socket_path)
                client_socket.sendall(json.dumps(data).encode())
                client_socket.close()
                messagebox.showinfo("成功", "图片数据已发送")
            except Exception as e:
                messagebox.showerror("错误", f"发送数据时出错: {e}")

if __name__ == "__main__":
    root = tk.Tk()
    app = ScreenSplitAssistTester(root)
    root.mainloop()
